/*----------------------------------------------------------\\
|| Mancala                                                  ||
|| CS-340 with Professor Troy                               ||
|| Joshua Jung and Eugene Magdel                            ||
|| April 29, 2011                                           ||
\\----------------------------------------------------------*/
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
#include "math.h"

#include <QGraphicsPixmapItem>

using namespace std;

#ifndef GEM_H
#define GEM_H
  class Gem{
    private:
      float pmi_x;
      float pmi_y;

      float next_pmi_x;
      float next_pmi_y;
    public:
      QGraphicsPixmapItem *pmi;

      float getX() {return next_pmi_x;}
      float getY() {return next_pmi_y;}

      void setPos(float x, float y)
      {
          if (pmi_x == -1)
          {
              pmi_x = x;
              pmi_y = y;

              next_pmi_x = pmi_x;
              next_pmi_y = pmi_y;
          }
          else
          {
              next_pmi_x = x;
              next_pmi_y = y;
          }
      }

      void updatePos(float interval)
      {
          double xdis = next_pmi_x - pmi_x;
          double ydis = next_pmi_y - pmi_y;

          double dis = sqrt(xdis * xdis + ydis * ydis);

          if (dis > 4)
          {
              double rise_over_run = fabs(ydis / xdis);

              if (rise_over_run > 1)
              {
                  rise_over_run = 1;
              }
              else if (rise_over_run < -1)
              {
                  rise_over_run = -1;
              }

              if (xdis > 5.0)
                  pmi_x += std::max(std::min(dis / 15, 8.0), 4.0);
              else if (xdis < -5.0)
                  pmi_x -= std::max(std::min(dis / 15, 8.0), 4.0);

              if (ydis > 5.0)
                  pmi_y += std::max(std::min((dis / 15) * rise_over_run, 8.0 * rise_over_run), 4.0 * rise_over_run);
              else if (ydis < -5.0)
                  pmi_y -= std::max(std::min((dis / 15) * rise_over_run, 8.0 * rise_over_run), 4.0 * rise_over_run);
          }
          else
          {
              next_pmi_x = pmi_x;
              next_pmi_y = pmi_y;
          }

          if (pmi != NULL)
          {
              pmi->setX(pmi_x);
              pmi->setY(pmi_y);
          }
      }

      string img_src;
      Gem();
      Gem * next;
      static const string image_sources[1];
  };

#endif
